package 狂神.GUI.listen;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @ClassName: TestActionTwo
 * @Auther: Drawin
 * @Date: 2021/12/23 21:08
 * @Description:
 */

public class TestActionTwo {
    public static void main(String[] args) {
        //两个按钮，实现同一个监听
        Frame frame = new Frame("开始——停止");
        Button button1 = new Button("start");
        Button button2 = new Button("stop");

        button2.setActionCommand("button-stop");//显示定义触发会返回的命令

        MyMonitor myMonitor = new MyMonitor();

        button1.addActionListener(myMonitor);
        button2.addActionListener(myMonitor);

        frame.add(button1,BorderLayout.NORTH);
        frame.add(button2,BorderLayout.SOUTH);

        frame.pack();
        frame.setVisible(true);
        lesson2.windowClose(frame);
    }
}


class MyMonitor implements ActionListener{
//两个按钮都只用了这一个监听类
    @Override
    public void actionPerformed(ActionEvent e) {
        //e.getActionCommand()获得按钮的信息
        System.out.println("按钮被点击了:m=>"+e.getActionCommand());
    }
}